﻿using JSM.DataStore;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSM.Test
{
	[TestClass]
	public class JSMHtmlHelperTest
	{
		[TestMethod]
		public void TestGetInitReadyJavaScript()
		{
			Assert.AreEqual("JSMController.InitReady();", JSMHtmlHelper.GetInitReadyJavaScript());
		}

		[TestMethod]
		public void TestGetInitJavaScriptWithDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithDefaultDataStore(TestGetInitJavaScript);
		}

		[TestMethod]
		public void TestGetInitJavaScriptWithoutDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithoutDefaultDataStore(TestGetInitJavaScript);
		}

		[TestMethod]
		public void TestGetJavaScriptModelContainerWithDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithDefaultDataStore(TestGetJavaScriptModelContainer);
		}

		[TestMethod]
		public void TestGetJavaScriptModelContainerWithoutDefaultDataStore()
		{
			UnitTestHelper.TestFunctionWithoutDefaultDataStore(TestGetJavaScriptModelContainer);
		}

		private void TestGetInitJavaScript(IJavaScriptModelAware javaScriptModelAware, IDataStoreProvider dataStoreProvider)
		{
			FillJavaScriptModel(javaScriptModelAware, dataStoreProvider);
			Assert.AreEqual("JSMController.Init({\"Test2\":true}, [{\"Function\":\"TestFunction2Before\",\"Parameters\":[{\"SomeProperty\":true,\"AnotherProperty\":\"Hello\"}]},{\"Function\":\"TestFunction2\",\"Parameters\":[true,123]}]);", JSMHtmlHelper.GetInitJavaScript(javaScriptModelAware, dataStoreProvider));
		}

		private void TestGetJavaScriptModelContainer(IJavaScriptModelAware javaScriptModelAware, IDataStoreProvider dataStoreProvider)
		{
			FillJavaScriptModel(javaScriptModelAware, dataStoreProvider);
			Assert.AreEqual("<div id=\"JSMVariables\" style=\"display: none;\"><!--[if true]>{\"Test\":42}<![endif]--></div><div id=\"JSMFunctions\" style=\"display: none;\"><!--[if true]>[{\"Function\":\"TestFunctionBefore\",\"Parameters\":[\"test\"]},{\"Function\":\"TestFunction\",\"Parameters\":null}]<![endif]--></div>", JSMHtmlHelper.GetJavaScriptModelContainer(javaScriptModelAware, dataStoreProvider));
		}

		private void FillJavaScriptModel(IJavaScriptModelAware javaScriptModelAware, IDataStoreProvider dataStoreProvider)
		{
			javaScriptModelAware.AddJavaScriptVariable("Test", 42, PageLoadEvent.Ready, dataStoreProvider);
			javaScriptModelAware.AddJavaScriptVariable("Test2", true, PageLoadEvent.Init, dataStoreProvider);
			javaScriptModelAware.AddJavaScriptFunction("TestFunction", PageLoadEvent.Ready, dataStoreProvider);
			javaScriptModelAware.InsertJavaScriptFunction("TestFunctionBefore", PageLoadEvent.Ready, dataStoreProvider, "test");
			javaScriptModelAware.AddJavaScriptFunction("TestFunction2", PageLoadEvent.Init, dataStoreProvider, true, 123);
			javaScriptModelAware.InsertJavaScriptFunction("TestFunction2Before", PageLoadEvent.Init, dataStoreProvider, new { SomeProperty = true, AnotherProperty = "Hello"});
		}
	}
}
